package com.cloudcc.mobile.manager;

import android.content.Context;
import android.content.SharedPreferences.Editor;

import com.cloudcc.cloudframe.model.push.PushBindModel;
import com.cloudcc.mobile.AppConfig;
import com.cloudcc.mobile.AppContext;
import com.cloudcc.mobile.util.PreferenceUtils;
import com.umeng.message.IUmengCallback;

/**
 * @Description: 消息推送管理器
 * @author wangzhiqiang
 * @date 2015-11-9 上午9:54:54
 * 
 */
public class CloudCCPushManager {

	private static CloudCCPushManager instance;

	private Context context;

	private CloudCCPushManager(Context context) {
		this.context = context;
	}

	public synchronized static CloudCCPushManager getInstance() {
		if (instance == null) {
			instance = new CloudCCPushManager(AppContext.getInstance());
		}
		return instance;
	}

	/**
	 * 恢复消息推送
	 */
	public void resumePushWork() {
		AppContext.mPushAgent.enable(new IUmengCallback() {
			@Override
			public void onSuccess() {
			}

			@Override
			public void onFailure(String s, String s1) {
			}
		});
	}

	/**
	 * 停止消息推送
	 */
	public void stopPushWork() {
		AppContext.mPushAgent.disable(new IUmengCallback() {
			@Override
			public void onSuccess() {
			}

			@Override
			public void onFailure(String s, String s1) {
			}
		});
	}

	/**
	 * 设置地图的绑定数据
	 * 
	 * @param binding
	 */
	public void setPushBind(PushBindModel binding) {
		if (binding == null) {
			return;
		}
		Editor editor = PreferenceUtils.getEditor(context, AppConfig.XML_PUSH);
		editor.putString(IKey.PUSH_USERID, binding.userId)
				.putString(IKey.PUSH_CHANNELD, binding.channelId)
				.putString(IKey.PUSH_APPID, binding.appid)
				.putString(IKey.PUSH_REQUESTID, binding.requestId).commit();
	}

	/**
	 * 获得 消息推送的绑定
	 * 
	 * @return
	 */
	public PushBindModel getPushBind() {
		PushBindModel pushBindModel = new PushBindModel();
		pushBindModel.userId = PreferenceUtils.getString(context,
				AppConfig.XML_PUSH, IKey.PUSH_USERID, "");
		pushBindModel.channelId = PreferenceUtils.getString(context,
				AppConfig.XML_PUSH, IKey.PUSH_CHANNELD, "");
		pushBindModel.appid = PreferenceUtils.getString(context,
				AppConfig.XML_PUSH, IKey.PUSH_APPID, "");
		pushBindModel.requestId = PreferenceUtils.getString(context,
				AppConfig.XML_PUSH, IKey.PUSH_REQUESTID, "");
		return pushBindModel;
	}

	public interface IKey {

		/**
		 * 保存的类型
		 */
		String DYNAMIC_TYPE = "dynamic.type";

		/**
		 * 推送数据
		 */
		String PUSH = "push";
		/**
		 * push channel_id
		 */
		String PUSH_CHANNELD = "push.channelId";
		/**
		 * push user_id
		 */
		String PUSH_USERID = "push.userId";
		/**
		 * push request_id
		 */
		String PUSH_REQUESTID = "push.requestId";
		/**
		 * push appid
		 */
		String PUSH_APPID = "push.appid";

		/**
		 * 快速回复微贴的 类型
		 */
		String CHATTER_EVENTTYPES = "chatter.event.type";

	}
}
